package com.paishen.peiniwan.module.login;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.paishen.common.util.BaseUtil;
import com.paishen.common.util.SpfsUtil;
import com.paishen.peiniwan.App;
import com.paishen.peiniwan.BaseActivity;
import com.paishen.peiniwan.BusEvent;
import com.paishen.peiniwan.C;
import com.paishen.peiniwan.R;
import com.paishen.peiniwan.database.UserDao;
import com.paishen.peiniwan.database.util.ModelConverter;
import com.paishen.peiniwan.frame.PnwOkCallbackAdapter;
import com.paishen.peiniwan.frame.http.PnwHttpManager;
import com.paishen.peiniwan.frame.http.entity.User;
import com.paishen.peiniwan.frame.http.response.HttpRespData;
import com.paishen.peiniwan.frame.widget.InputEditLayout;

import de.greenrobot.event.EventBus;

public class RegisterSetPwdActivity extends BaseActivity {
    public static final String EXTRA_AUTH_CODE = "auth_code";
    private String authCode;

    private String phone;

    private InputEditLayout layEditPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        phone = getIntent().getStringExtra(RegisterCompleteActivity.EXTRA_PHONE);
        authCode = getIntent().getStringExtra(EXTRA_AUTH_CODE);

        setContentView(R.layout.activity_register_set_pwd);

        BaseUtil.setStatusBarDarkMode(true, this);

        findViewById(R.id.lay_container).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseUtil.hideSoftInputFromWindowAlways(RegisterSetPwdActivity.this);
            }
        });

        layEditPassword = (InputEditLayout) findViewById(R.id.lay_edit_set_pwd);
        layEditPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

        View imgBack = findViewById(R.id.img_back);
        ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) imgBack.getLayoutParams();
        int fixHeight = BaseUtil.dip2px(4);
        lp.setMargins(0, fixHeight + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ? BaseUtil.getStatusBarHeight() : 0), 0, 0);
        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

            TextView txSubmit = (TextView) findViewById(R.id.tx_next);
            txSubmit.setText(R.string.finish);

        findViewById(R.id.tx_next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String inputPassword = layEditPassword.getText().toString();
                if (inputPassword.length() < 6) {
                    BaseUtil.showToast(R.string.register_input_set_pwd);
                    return;
                }

                    requestForgetPassword(authCode, phone, inputPassword);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BaseUtil.hideSoftInputFromWindowAlways(this);
    }

    private void requestForgetPassword(String vcode, final String mobile, final String pwd) {
        PnwHttpManager.User.forgetPassword(TAG, vcode, mobile, pwd, new PnwOkCallbackAdapter<HttpRespData<User>>() {
            @Override
            public void onResponse(HttpRespData<User> response, boolean fresh) {
                super.onResponse(response, fresh);
                if (response.errcode == C.http.RESULT_SUCCESS) {
                    SpfsUtil.write(C.sp.CURRENT_LOGIN_USER_ID, response.data.id);
                    final UserDao userDao = App.i().getDaoSession(response.data.id).getUserDao();
                    com.paishen.peiniwan.database.User dbUser = userDao.queryBuilder().where(UserDao.Properties.Id.eq(response.data.id)).unique();
                    dbUser = ModelConverter.mergeOrReplace(dbUser, response.data);
                    dbUser.setPhone(mobile);
                    dbUser.setPassword(pwd);
                    dbUser.setApp_auto_login(false);
                    App.i().setUser(dbUser);
                    EventBus.getDefault().post(new BusEvent(BusEvent.TYPE_BACKGROUND, BusEvent.ACTION_DB_UPDATE_USER, dbUser));
                    EventBus.getDefault().post(new BusEvent( BusEvent.ACTION_LOGIN_AUTO_INPUT));

                    finish();
                } else if (fresh && response.errcode != C.http.RESULT_LOGIN_EXPIRE &&
                        response.errcode != C.http.RESULT_LOGIN_OFFLINE) {
                    BaseUtil.showToast(response.errmsg);
                }
            }
        });
    }
}
